<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<script src="./js/jquery-1.7.2.min.js"></script>
</head>
<style>
</style>
<script>

$(document).ready(function () {

var now = new Date();
	$("#date").text(now.getFullYear()+"-"+(now.getMonth()+1)+"-"+now.getDate() + "  "+now.getHours()+":"+now.getMinutes()+":"+now.getSeconds());

	$("#printTest").click(function(){
		printTest();
	});
	
	$("#generate").click(function(){
		heng();
		shu();
	});
});

function heng(){
	var options = {
		total:$("#hengTotal").val()
		,innerTenPlus:$("#innerTenPlus:checked").val()
		,innerTwentyPlus:$("#innerTwentyPlus:checked").val()
		,innerTenMinus:$("#innerTenMinus:checked").val()
		,innerTwentyMinus:$("#innerTwentyMinus:checked").val()
		,threePlus:$("#threePlus:checked").val()
		,threePlusMinus:$("#threePlusMinus:checked").val()
		,threeMinus:$("#threeMinus:checked").val()
		,fillTen:$("#fillTen:checked").val()
		,mutilable10:$("#mutilable10:checked").val()
		,fillTwenty:$("#fillTwenty:checked").val()
	}
	
	options.numberList = [];
	options.nameList = [];
	var totalCount = 0;
	var endCount;
	$(".heng:checked").each(function(i,n){
		endCount = totalCount + $(n).val() * 100;
		options.numberList.push({"begin":totalCount , "end":endCount});
		options.nameList.push($(n).attr("attr"));
		totalCount = endCount;
		options.totalCount = endCount;
	});
	
	if(options.nameList.length>0){
		var testList = generatHengTest(options);
	
		$("#hengTestList").empty();
		for(var i = 0 ; i<testList.length ; i++){
			var testItem = $("<div>").addClass("hengTestItem").text(testList[i]);
			$("#hengTestList").append(testItem);
		}
	}
	
}

function generatHengTest(options){
		
	if(options.nameList.length<=0){
		alert("至少选择一个题目类别");
		return;
	}
	var result = [];
	var sumCount = options.total;
	
	while(sumCount >0 ){
		var random = Math.round(Math.random() * (options.totalCount-1));
		var number = 0;
		for(var i = options.numberList.length - 1  ; i >= 0;i--){
			if(random >= options.numberList[i].begin && random<= options.numberList[i].end)
			{
				number = i;
				break;
			}
		}
		var targetScript = options.nameList[number]+"();";
		var item = eval(targetScript);
		
		
		if(!isExist(result , item)  && item){
			result.push(item);
			sumCount --;	
		}
	}

	return result;
}

function shu(){
	var options = {
		total:$("#listTotal").val()
		,innerTwenty:$("#innerTwenty:checked").val()
		,innerFifty:$("#innerFifty:checked").val()
		,innerHundred:$("#innerHundred:checked").val()
	}
	
	options.totalCount = 0;
	options.numberList = [];
	options.nameList = [];
	$(".shu:checked").each(function(i,n){
		options.totalCount += $(n).val() * 100;
		options.numberList.push(options.totalCount);
		options.nameList.push($(n).attr("attr"));
	});
	
	if(options.nameList.length>0){
		var testList = generatShuTest(options);
	
		$("#listTestList").empty();
		
		for(var i = 0 ; i<testList.length ; i++){
			showShuTest(testList[i]);
		}
	}
	
}

function generatShuTest(options){
		
		if(options.nameList.length<=0){
			alert("至少选择一个题目类别");
			return;
		}
		var result = [];
		var sumCount = options.total;
		
		while(sumCount >0 ){
			var random = Math.round(Math.random() * (options.totalCount-1));
			var number = 0;
			for(var i = options.numberList.length ; i >= 0;i--){
				if(random >= options.numberList[i-1])
				{
					number = i;
					break;
				}
			}
			var targetScript = options.nameList[number]+"();";
			var item = eval(targetScript);
			
			
			if(!isExist(result , item)){
				result.push(item);
				sumCount --;	
			}
		}

		return result;
		
	}

function isExist(result , testItem){
	for(var i = 0;i<result.length;i++){
		if(result[i] == testItem){
		console.log(testItem);
			return true;
		}
	}
	return false;
}

function showShuTest(testItem){
	
	var testTable = $("<table></table>").addClass("shuTestItem");
	var tr = $("<tr></tr>");
	tr.append($("<td></td>"));
	tr.append($("<td></td>").attr("align","right").addClass("fontClass").text(testItem.left));
	testTable.append(tr);
	var tr2 = $("<tr></tr>");
	tr2.append($("<td></td>").attr("align" , "left").addClass("fontClass").text(testItem.flag));
	tr2.append($("<td></td>").attr("align" , "right").addClass("fontClass").text(testItem.right));
	testTable.append(tr2);
	var tr3 = $("<tr></tr>");
	tr3.append($("<td></td>").attr("colspan" , 2).append($("<div></div>").addClass("line")));
	testTable.append(tr3);
	$("#listTestList").append(testTable);
	
}

function printTest(){
  window.print(); 
}

//有进位的加法
function tenPlus(){
	var one,two;
	two = 1+Math.round(Math.random() * 8);
	one = 10 + Math.round(Math.random() * (two-1));
	return {
	left:one,flag:"+",right:two
	}
}

//有退位的减法
function tenMinus(){
	var one,two;
	two = 1+Math.round(Math.random() * 8);
	one = 10 + Math.round(Math.random() * (two-1));
	return {
	left:one,flag:"-",right:two
	}
}
//20以内加减法
function innerTwenty(){
	var flag = Math.round(Math.random() * 1000);
	var one , two , flagStr;
	if(flag % 2 == 0){
		//加法
		flagStr = "+";
		two = 1+Math.round(Math.random() * 8);
		one = 1+Math.round(Math.random() * 8);
		
	}else{
		flagStr = "-";
		var number1 = 1+Math.round(Math.random() * 8);
		var number2 = 10+Math.round(Math.random() * 9);
		one = number1>number2?number1:number2;
		two = number1<number2?number1:number2;
	}
	return {
	left:one,flag:flagStr,right:two
	}
}

function innerFifty(){
	var flag = 1+Math.round(Math.random());
	var one , two , flagStr;
	if(flag>1){
		//加法
		flagStr = "+";
		two = 10+Math.round(Math.random() * 40);
		one = 10+Math.round(Math.random() * 40);
		
	}else{
		flagStr = "-";
		var number1 = 10+Math.round(Math.random() * 40);
		var number2 = 10+Math.round(Math.random() * 40);
		one = number1>number2?number1:number2;
		two = number1<number2?number1:number2;
	}
	return {
	left:one,flag:flagStr,right:two
	}
}

function innerHundred(){
	var flag = 1+Math.round(Math.random());
	var one , two , flagStr;
	if(flag>1){
		//加法
		flagStr = "+";
		two = 10+Math.round(Math.random() * 90);
		one = 10+Math.round(Math.random() * 90);
		
	}else{
		flagStr = "-";
		var number1 = 10+Math.round(Math.random() * 90);
		var number2 = 10+Math.round(Math.random() * 90);
		one = number1>number2?number1:number2;
		two = number1<number2?number1:number2;
	}
	return {
	left:one,flag:flagStr,right:two
	}
}



//无进位加法
function innerTenPlus(){
	var one = 1 + Math.round(Math.random() * 9);
	var two = 1 + Math.round(Math.random() * (10-one));
	return one + " + " + two+" =";
}

//有进位加法
function innerTwentyPlus(){
	/*
	var sum = 4 + Math.round(Math.random() * 5);
	while(sum<=10){
		sum = 10 + Math.round(Math.random() * 10);
	}
	*/
	
	var one = 4 + Math.round(Math.random() * 5);
	var two = 4 + Math.round(Math.random() * 5);
	
	return one + " + " + two+" =";
}

//结果为10的加法
function plusTen(){
	var one = 1 + Math.round(Math.random() * 9);
	var two = 10-one;
	return one + " + " + two+" =";
}

//10的减法
function minusTen(){
	var one = 10;
	var two = 1+Math.round(Math.random() * (one-1));
	return one + " - " + two+" =";
}


//无退位减法
function innerTenMinus(){
	var one = 5 + Math.round(Math.random() * 5);
	var two = 1+Math.round(Math.random() * (one-1));
	
	return one + " - " + two+" =";
}

//有退位减法
function innerTwentyMinus(){
	var one = 10 + Math.round(Math.random() * 4);
	var two = 5 + Math.round(Math.random() * 4);
	
	return one + " - " + two+" =";
}

function threePlus(){
	var one = 1+Math.round(Math.random() * 8);
	var two = 1+Math.round(Math.random() * 8);
	var three = 1+Math.round(Math.random() * 8);
	
	return one + " + " + two + " + " + three +" =";
}

function threePlusMinus(){
	var one = 1+Math.round(Math.random() * 8);
	var two = 1+Math.round(Math.random() * 8);
	var three = 1+Math.round(Math.random() * 8);
	
	if(one>=two){
		if((one + two - three)>0)
		{
			return one + " + " + two + " - " + three +" =";
		}else if((one - two + three)>0){
			return one + " - " + two + " + " + three +" =";
		}

	}else{
		if((one + two - three)>0){
			return one + " + " + two + " - " + three +" =";
		}

	}
}

function threeMinus(){
	var two = 1+Math.round(Math.random() * 8);
	var three = 1+Math.round(Math.random() * 8);
	
	var one = 0 ;
	while(one<=(two + three)){
		one = Math.round(Math.random() * (two + three));
		var random = 1+Math.round(Math.random() * (19-one));
		one = one + random;
	}
	
	return one + " - " + two + " - " + three +" =";
}

//2项填空
function fillTen(){
	//var flag = 1+Math.round(Math.random());
	var flag = 2;
	var fillNum = 1+Math.round(Math.random());
	var one , two , flagStr , total;
	if(flag>1){
		//加法
		flagStr = " + ";
		if(fillNum>1){
			//填空在前
			one = "____";
			total = 3+Math.round(Math.random() * 7);
			two = 1+Math.round(Math.random() * (total-2));
		}else{
			//填空在后
			two = "____";
			total = 3+Math.round(Math.random() * 7);
			one = 1+Math.round(Math.random() * (total-2));
		}
	}else{
		//减法
		flagStr = " - ";
		if(fillNum>1){
			//填空在前
			one = "____";
			total = 1+Math.round(Math.random() * 4);
			two = 1+Math.round(Math.random() * (9 - total));
		}else{
			//填空在后
			two = "____";
			total = 1+Math.round(Math.random() * 6);
			one = total+Math.round(Math.random() * (9-total));
		}
	}
	return one + flagStr + two +" = " + total;
}

function fillTwenty(){
	//var flag = 1+Math.round(Math.random());
	var flag = 2;
	var fillNum = 1+Math.round(Math.random());
	var one , two , flagStr , total;
	if(flag>1){
		//加法
		flagStr = " + ";
		if(fillNum>1){
			//填空在前
			one = "____";
			total = 5+Math.round(Math.random() * 14);
			two = Math.round(Math.random() * (total-1));
		}else{
			//填空在后
			two = "____";
			total = 5+Math.round(Math.random() * 14);
			one = Math.round(Math.random() * (total-1));
		}
	}else{
		//减法
		flagStr = " - ";
		if(fillNum>1){
			//填空在前
			one = "____";
			two = 1+Math.round(Math.random() * 8);
			total = 1+Math.round(Math.random() * 8);
		}else{
			//填空在后
			two = "____";
			total = 1+Math.round(Math.random() * 4);
			one = 9+Math.round(Math.random() * total);
		}
	}
	return one + flagStr + two +" = " + total;
}

function mutilable10(){
	var one = 1 + Math.round(Math.random() * 8);
	var two = 1 + Math.round(Math.random() * 8);;
	return one + " Ｘ " + two+" =";
}
</script>

<style>

.fontClass{
	font-size : 20px;
	font-weight : blod;
	letter-spacing : 8px;
}

.configItem{
	display:inline;
	float:left;
	margin-right:10px
}

.shuTestItem{
	float:left;
	
	margin-left:30px;
	margin-bottom:40px;
	width:80px;
}

div.row{
margin-top:30px;
}

div.line{
height:5px;
/* background-image:url('./line.png');*/
border-top:2px solid black;
width:100%;
}

.hengTestItem{
	float:left;
	width:30%;
	font-size:18px;
	padding-left:5px;
	text-align : left;
	/* border: black 1px solid;*/
	height : 40px;
	vertical-align :center;
}
</style>

<body>
	<div>
		<div class="configItem" style="display:none">题目数量<input id="listTotal" type="hidden" maxLength="3" value="0"><input id="hengTotal" type="hidden" maxLength="3" value="120"></div>
		
		<div class="configItem"><input id="innerTwentyPlus" class="item heng" attr="innerTwentyPlus" type="checkbox" value="1"/>20以内加法</div>
		<div class="configItem"><input id="plusTen" class="item heng" attr="plusTen" type="checkbox" value="1"/>等于10加法</div>
		<div class="configItem"><input id="minusTen" class="item heng" attr="minusTen" type="checkbox" value="1"/>10的减法</div>
		<div class="configItem"><input id="innerTwentyMinus" class="item heng" attr="innerTwentyMinus" checked type="checkbox" value="3"/>有退位减法</div>
		<div class="configItem"><input id="threePlus" class="item heng" attr="threePlus" type="checkbox" checked value="1"/>20以内三项加法</div>
		<div class="configItem"><input id="threePlusMinus" class="item heng" attr="threePlusMinus" checked type="checkbox" value="0.5"/>20以内三项加减法</div>
		<div class="configItem"><input id="threeMinus" class="item heng" attr="threeMinus" type="checkbox" checked value="1"/>20以内三项减法</div>
		<div class="configItem"><input id="fillTen" class="item heng" attr="fillTen" type="checkbox" value="1"/>10以内加法填空</div>
		<div class="configItem"><input id="fillTwenty" class="item heng" attr="fillTwenty" type="checkbox"  value="1"/>20以内填空</div>
		<div class="configItem"><input id="mutilable10" class="item heng" attr="mutilable10"  type="checkbox" value="1"/>10以内乘法</div>
		
		<div class="configItem"><input id="innerTwenty" class="item shu" attr="innerTwenty"  type="checkbox" value="1"/>shu-20以内加减法</div>
		<div class="configItem"><input id="innerFifty" class="item  shu" attr="innerFifty"  type="checkbox" value="1"/>shu-50以内加减法</div>
		<div class="configItem"><input id="innerHundred" class="item  shu" attr="innerHundred"  type="checkbox" value="1"/>shu-100以内加减法</div>
	</div>
	<div>
		<input type="button" id="generate" value="生成">
		<input type="button" id="printTest" value="打印">
		<span id="date"></span>
	</div>
	<br>
	
	<!--startprint-->
	<div  id="hengTestList">
		
	</div>
	
	<div id="listTestList">
		
	</div>
	<!--endprint-->
</body>